package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.CardInfo;
import com.example.entity.UserInfo;
import com.example.mapper.TradeInfoMapper;
import com.example.mapper.UserInfoMapper;
import com.example.service.CardInfoService;
import com.example.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 客户信息服务实现类
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private CardInfoService cardInfoService;
    
    @Autowired
    private TradeInfoMapper tradeInfoMapper;

    @Override
    @Transactional
    public boolean removeById(Serializable customerId) {
        // 1. 查询该客户的所有银行卡
        List<CardInfo> cardList = cardInfoService.list(
            new QueryWrapper<CardInfo>()
                .eq("customer_id", customerId)
        );
        
        // 2. 对每张银行卡，先删除其交易记录，再删除银行卡
        for (CardInfo card : cardList) {
            // 2.1 删除该银行卡的所有交易记录
            tradeInfoMapper.delete(
                new QueryWrapper<com.example.entity.TradeInfo>()
                    .eq("card_id", card.getCardId())
            );
            
            // 2.2 删除银行卡
            cardInfoService.removeById(card.getCardId());
        }
        
        // 3. 删除客户
        return super.removeById(customerId);
    }
} 